package com.example.shortvideobackend.controller;

import com.example.shortvideobackend.service.WebRTCSignalingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Controller
public class WebSocketController {

    @Autowired
    private WebRTCSignalingService signalingService;

    @MessageMapping("/stream/join")
    public void handleJoinStream(@Payload Map<String, Object> message, 
                                SimpMessageHeaderAccessor headerAccessor) {
        String sessionId = headerAccessor.getSessionId();
        String streamKey = (String) message.get("streamKey");
        String userId = (String) message.get("userId");
        
        signalingService.handleJoinStream(sessionId, streamKey, userId);
    }

    @MessageMapping("/stream/leave")
    public void handleLeaveStream(SimpMessageHeaderAccessor headerAccessor) {
        String sessionId = headerAccessor.getSessionId();
        signalingService.handleLeaveStream(sessionId);
    }

    @MessageMapping("/stream/offer")
    public void handleOffer(@Payload Map<String, Object> offer, 
                           SimpMessageHeaderAccessor headerAccessor) {
        String sessionId = headerAccessor.getSessionId();
        signalingService.handleOffer(sessionId, offer);
    }

    @MessageMapping("/stream/answer")
    public void handleAnswer(@Payload Map<String, Object> answer, 
                            SimpMessageHeaderAccessor headerAccessor) {
        String sessionId = headerAccessor.getSessionId();
        signalingService.handleAnswer(sessionId, answer);
    }

    @MessageMapping("/stream/ice-candidate")
    public void handleIceCandidate(@Payload Map<String, Object> candidate, 
                                  SimpMessageHeaderAccessor headerAccessor) {
        String sessionId = headerAccessor.getSessionId();
        signalingService.handleIceCandidate(sessionId, candidate);
    }
} 